package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.enums.HashRateOrderEnums;
import com.service.dao.enums.OrderProductOtherStatus;
import com.service.dao.mapper.OrderProductOtherMapper;
import com.service.dao.model.OrderProductHrCombo;
import com.service.dao.model.OrderProductOther;
import com.service.dao.model.SysConfig;
import com.service.scheduling.service.ConfigService;
import com.service.scheduling.service.OrderProductOtherService;
import com.service.scheduling.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年12月15日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
public class OrderProductOtherServiceImpl extends ServiceImpl<OrderProductOtherMapper, OrderProductOther> implements OrderProductOtherService {
    @Autowired
    private ConfigService configService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelOrder() {

        Date validDate = configService.getOrderOverdueTime();
        if (ObjectUtils.isEmpty(validDate)) {
            return;
        }
        log.info("cancelOrder 取消{}之前未支付订单 --- start",
                DateFormatUtils.format(validDate, "yyyy-MM-dd HH:mm:ss"));
        List<OrderProductOther> otherList = list(new QueryWrapper<OrderProductOther>().lambda()
                .eq(OrderProductOther::getOrderStatus, OrderProductOtherStatus.NOT_PAY.getCode())
                .le(OrderProductOther::getCreateTime, validDate));
        List<OrderProductOther> updateProductOtherOrder = otherList.stream().map(item -> {
            OrderProductOther updateOrder = new OrderProductOther();
            updateOrder.setId(item.getId());
            updateOrder.setOrderStatus(HashRateOrderEnums.Status.noneffective.getCode());
            return updateOrder;
        }).collect(Collectors.toList());

        if (ObjectUtils.isEmpty(updateProductOtherOrder)) {
            log.info("cancelOrder 取消{}之前未支付订单 数量 0 --- end",
                    DateFormatUtils.format(validDate, "yyyy-MM-dd HH:mm:ss"));
            return;
        }

        updateBatchById(updateProductOtherOrder);
        log.info("cancelOrder 取消{}之前未支付订单 数量 {} --- end",
                DateFormatUtils.format(validDate, "yyyy-MM-dd HH:mm:ss"), updateProductOtherOrder.size());

    }

    @Override
    public BigDecimal getSomedayGiftOrderTotalMoney(Integer memberId, Date someday) {
        Date someDayStartTime = DateUtil.getDayOfStart(someday);
        Date someDayEndTime = DateUtil.getDayOfEnd(someday);

        return baseMapper.getSomedayGiftOrderTotalMoney(memberId, OrderProductOtherStatus.getBuyStatus(), someDayStartTime, someDayEndTime);
    }

    @Override
    public BigDecimal getGiftOrderTotalMoneyByTime(Integer userId, Date startTime, Date endTime) {
        return baseMapper.getSomedayGiftOrderTotalMoney(userId, OrderProductOtherStatus.getBuyStatus(), startTime, endTime);
    }

    /**
     * 获取有效订单金额
     *
     * @param userId
     * @return
     */
    @Override
    public BigDecimal getGiftOrderTotalMoney(Integer userId) {
        return baseMapper.getGiftOrderTotalMoney(userId, OrderProductOtherStatus.getBuyStatus());
    }
}
